package com.newworld.dmsapp2.ui.view;

import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.text.format.Time;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.activity.serviceapply.WriteOutdoorPaperActivity;
import com.newworld.dmsapp2.activity.serviceapply.WritePaperActivity;
import com.newworld.dmsapp2.ui.view.DateAndTimeIntervalPickerDialog.OnDateSetListener;
import com.newworld.dmsapp2.util.Config;

/**
 * 自定义dialog
 * 
 * @author Administrator
 * 
 */
// 使用样例
/*
 * final CustomDialog dialog = new CustomDialog(LoginActivity.this);
 * dialog.setTitle("标题"); dialog.setDetial("内容"); dialog.setLeftOnClick(new
 * OnClickListener(){
 * 
 * @Override public void onClick(View v) { dialog.dismiss(); }}); dialog.show();
 */
public class CustomEditServiceStyleDialog {
	private TextView title;
	private RadioButton radioButton_expect, radioButton_prompt;
	private Dialog mDialog;
	private String str;
	private LayoutInflater mInflater;
	private OnStateChagedListener1 mCallBack;
	//private Context context;
	private Calendar cs;
	private Context context;
	// 接口
	public interface OnStateChagedListener1 {
		void onStateChanged(String str);
	}

	public void setOnStateChagedListener1(OnStateChagedListener1 mCallBack) {
		this.mCallBack = mCallBack;
	}

	public CustomEditServiceStyleDialog(final Context context) {
		this.context = context;
		mDialog = new Dialog(context, R.style.customDialog); // 一个无title样式
		mInflater = LayoutInflater.from(context);
		View dialogView = mInflater.inflate(R.layout.custom_edit_service_style,
				null);

		Window window = mDialog.getWindow();
		window.setContentView(dialogView);

		findView(window);

		// RadioGroupListener listener=new RadioGroupListener();
		// radioGroup_sex.setOnCheckedChangeListener(listener);
		radioButton_prompt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				str = "及时上门";
				Time t = new Time();
				t.setToNow();
				int currenthour = t.hour;
				int currenthourmax = currenthour + 1;
				int nowyearint = t.year;
				int nowmonthint = t.month + 1;
				int nowdayint = t.monthDay;
				System.out.print("当前小时为：" + currenthour);
				String dayString;
				String timeInterval;
				String yearstr,monthstr,daystr;
				if (nowyearint < 10) {
					yearstr = "0" + nowyearint;
				} else {
					yearstr = nowyearint + "";
				}
				if (nowmonthint < 10) {
					monthstr = "0" + nowmonthint;

				} else {
					monthstr = nowmonthint + "";
				}
				if (nowdayint < 10) {
					daystr = "0" + nowdayint;
				} else {
					daystr = nowdayint + "";
				}
				if(currenthourmax < 13){
					currenthourmax = 15;
					timeInterval =  "14:00~15:00" ;
					dayString = yearstr + "-" + monthstr + "-"
						+ daystr;
				} else{
					currenthourmax = 9;
					timeInterval = "08:00~09:00";
					nowdayint = nowdayint +1;
					if (nowdayint < 10) {
						daystr = "0" + nowdayint;
					} else {
						daystr = nowdayint + "";
					}
					dayString = yearstr + "-" + monthstr + "-"
						+ daystr;
				}
				if(Config.AREAFLAG){
					WriteOutdoorPaperActivity.outdoor_ser_start_time.setText("及时上门");
					WriteOutdoorPaperActivity.outdoor_ser_start_time_str = dayString + " "
							+ timeInterval;
				} else{
					WritePaperActivity.ser_start_time.setText("及时上门");
					WritePaperActivity.ser_start_time_str = dayString + " "
							+ timeInterval;
				}
				radioButton_prompt.setChecked(true);
				radioButton_expect.setChecked(false);
				tryNotifyStateChanged();

			}
		});
		radioButton_expect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				str = "预约时间";
				radioButton_expect.setChecked(true);
				radioButton_prompt.setChecked(false);
				cs = Calendar.getInstance();
				new DateAndTimeIntervalPickerDialog(context,
				new OnDateSetListener() {

					private Toast toast;

					@Override
					public void onDateSet(Calendar calendar,
							String timeInterval) {
						// TODO Auto-generated method stub
						Date dateNow = new Date();

						String dayString;
						String yearstr, monthstr, daystr;

						int yearint, monthint, dayint;

						String hour = timeInterval.substring(0, 2);
						Time t = new Time();
						t.setToNow();
						int currenthour = t.hour;
						int currenthourmax = currenthour + 1;
						int nowyearint = t.year;
						int nowmonthint = t.month + 1;
						int nowdayint = t.monthDay;
						int mhour = Integer.parseInt(hour);
						System.out.print("当前小时为：" + currenthour);
						System.out.print("日历小时为：" + hour);

						yearint = calendar.get(Calendar.YEAR);
						monthint = calendar.get(Calendar.MONTH) + 1;
						dayint = calendar.get(Calendar.DAY_OF_MONTH);
						Log.e("选择时间", yearint + "+" + monthint + "+" + dayint);
						calendar.set(yearint, monthint - 1, dayint, mhour,
								0, 0);
						Date date = calendar.getTime();
						int daybetween = daysBetween(dateNow, date);
						int timebetween = timeBetween(dateNow, date);
						System.out.println("daybetween____" + daybetween);
						Log.e("WriteOutdoorPaperActivity>>>daybetween",
								"daybetween = " + daybetween);
						if (date.before(dateNow)
								|| ((yearint == nowyearint)
										&& (monthint == nowmonthint)
										&& (dayint == nowdayint) && (timebetween < 2))) {
							toast = Toast.makeText(
									context,
									"请选择当前时间两个小时之后的时间", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
							if (nowyearint < 10) {
								yearstr = "0" + yearint;
							} else {
								yearstr = yearint + "";
							}
							if (monthint < 10) {
								monthstr = "0" + monthint;

							} else {
								monthstr = monthint + "";
							}
							if (dayint < 10) {
								daystr = "0" + dayint;
							} else {
								daystr = dayint + "";
							}
							if (currenthour < 9) {
								timeInterval = "0" + currenthour + ":00"
										+ "~" + "0" + currenthourmax
										+ ":00";
							} else if (currenthour == 9) {
								timeInterval = "0" + currenthour + ":00"
										+ "~" + currenthourmax + ":00";
							} else {
								timeInterval = currenthour + ":00" + "~"
										+ currenthourmax + ":00";
							}
							dayString = yearstr + "-" + monthstr + "-"
									+ daystr;
							if(Config.AREAFLAG){
								WriteOutdoorPaperActivity.outdoor_ser_start_time.setText(dayString + " "
										+ timeInterval);
								WriteOutdoorPaperActivity.outdoor_ser_start_time_str = dayString + " "
										+ timeInterval;
							} else{
								WritePaperActivity.ser_start_time.setText(dayString + " "
										+ timeInterval);
								WritePaperActivity.ser_start_time_str = dayString + " "
										+ timeInterval;
							}
							

						} else if (daybetween <= 6) {// 从当天开始计算
							if (yearint < 10) {
								yearstr = "0" + yearint;
							} else {
								yearstr = yearint + "";
							}
							
							if (monthint < 10) {
								if (monthint == 1) {
									// nowmonthint = nowmonthint+1;
									monthstr = "0" + monthint;
								}
								monthstr = "0" + monthint;

							} else {
								monthstr = monthint + "";
							}
							if (dayint < 10) {
								daystr = "0" + dayint;
							} else {
								daystr = dayint + "";
							}
							dayString = yearstr + "-" + monthstr + "-"
									+ daystr;
							/*WriteOutdoorPaperActivity.ser_start_time.setText(dayString + " "
									+ timeInterval);*/
							if(Config.AREAFLAG){
								WriteOutdoorPaperActivity.outdoor_ser_start_time.setText(dayString + " "
										+ timeInterval);
								WriteOutdoorPaperActivity.outdoor_ser_start_time_str = dayString + " "
										+ timeInterval;
							} else{
								WritePaperActivity.ser_start_time.setText(dayString + " "
										+ timeInterval);
								WritePaperActivity.ser_start_time_str = dayString + " "
										+ timeInterval;
							}
						} else {
							toast = Toast.makeText(
									context,
									"请选择未来七天之内的时间", Toast.LENGTH_SHORT);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
							if (yearint < 10) {
								yearstr = "0" + yearint;
							} else {
								yearstr = yearint + "";
							}
							if (monthint < 10) {
								monthstr = "0" + monthint;
							} else {
								monthstr = monthint + "";
							}
							if (dayint < 10) {
								daystr = "0" + dayint;
							} else {
								daystr = dayint + "";
							}
							if (currenthour < 9) {
								timeInterval = "0" + currenthour + ":00"
										+ "~" + "0" + currenthourmax
										+ ":00";
							} else if (currenthour == 9) {
								timeInterval = "0" + currenthour + ":00"
										+ "~" + currenthourmax + ":00";
							} else {
								timeInterval = currenthour + ":00" + "~"
										+ currenthourmax + ":00";
							}
							dayString = yearstr + "-" + monthstr + "-"
									+ daystr;
							/*WriteOutdoorPaperActivity.ser_start_time.setText(dayString + " "
									+ timeInterval);*/
							if(Config.AREAFLAG){
								WriteOutdoorPaperActivity.outdoor_ser_start_time.setText(dayString + " "
										+ timeInterval);
								WriteOutdoorPaperActivity.outdoor_ser_start_time_str = dayString + " "
										+ timeInterval;
							} else{
								WritePaperActivity.ser_start_time.setText(dayString + " "
										+ timeInterval);
								WritePaperActivity.ser_start_time_str = dayString + " "
										+ timeInterval;
							}
						}
					}
				}, cs).show();

				
				tryNotifyStateChanged();
			}
		});
	}
	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算两个日期之间相差的小时数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int timeBetween(Date date1, Date date2) {
		long time1 = date1.getTime();
		long time2 = date2.getTime();
		long intervalMilli = time2 - time1;

		return (int) (intervalMilli / (1000 * 3600));
	}
	private void findView(Window window) {
		title = (TextView) window
				.findViewById(R.id.custom_edit_service_style_title);
		radioButton_prompt = (RadioButton) window
				.findViewById(R.id.radioButton_prompt);
		radioButton_expect = (RadioButton) window
				.findViewById(R.id.radioButton_expect);
	}

	class RadioGroupListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup radio1, int id1) {
			// TODO Auto-generated method stub
			if (id1 == radioButton_prompt.getId()) {
				// 把mRadio1的内容传到mTextView1
				tryNotifyStateChanged();

			} else if (id1 == radioButton_expect.getId()) {
				// 把mRadio2的内容传到mTextView1
				tryNotifyStateChanged();

			}
		}
	}


	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		this.title.setText(title);
	}

	/**
	 * 设置详情
	 * 
	 * @param detial
	 */
	@SuppressLint("NewApi")
	public void setDetial(String str) {

		this.str = str;
		if (str.equals("及时上门")) {
			radioButton_prompt.setChecked(true);
			radioButton_expect.setChecked(false);

		} else {
			radioButton_expect.setChecked(true);
			radioButton_prompt.setChecked(false);
		}
	}

	public String getDetial() {
		return str;
	}

	

	public void show() {
		mDialog.show();
	}

	public void dismiss() {
		mDialog.dismiss();
	}

	private void tryNotifyStateChanged() {
		if (mCallBack != null) {
			mCallBack.onStateChanged(str);
		}
	}
}
